package {
	import camo.core.events.LoaderManagerEvent;
	import camo.core.managers.CamoPropertySheetManager;
	import camo.core.managers.DecalSheetManager;
	
	import components.DecalDisplay;
	
	import container.WindowSelectAuto;
	import container.WindowWheelParameters;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import managers.GlobalDecalSheetManager;
	import managers.GlobalPropertySheetManager;
	
	[SWF( width="980", height="700", backgroundColor="#3A3E4A", framerate="24" )]
	
	public class FittingRoom extends Sprite
	{
		private var propSheet : CamoPropertySheetManager;
		private var brendAutoXml : XMLList; 
		
		public function FittingRoom(){
			loadFonts();
		}
		private function loadFonts():void{
			var fontSWF : Loader = new Loader();  
			fontSWF.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontsLoaded);  
			fontSWF.load(new URLRequest("swfs/fonts/FontLibrary.swf"));
		}
		private function onFontsLoaded(e:Event):void{
			e.target.removeEventListener(Event.COMPLETE, onFontsLoaded);
			
			/*var fontList:Array = Font.enumerateFonts(false);
			trace("Load font:",fontList.length); 
			for (var i:uint=0; i<fontList.length; i++) {
			trace("font: "+fontList[i].fontName+' style:'+fontList[i].fontStyle+' type:'+fontList[i].fontType);
			}*/
			
			loadPropertySheet();
		}
		
		private function loadPropertySheet():void{
			
			propSheet = GlobalPropertySheetManager.instance;  
			
			var loader : URLLoader = new URLLoader( );  
			loader.addEventListener( Event.COMPLETE, onPropertySheetLoad );  
			loader.load( new URLRequest("css/main.properties.css"));  
		}
		private function onPropertySheetLoad(e:Event):void{
			
			var target : URLLoader = e.target as URLLoader;  
			target.removeEventListener(Event.COMPLETE, onPropertySheetLoad);  
			
			propSheet.parseCSS("global.properties", target.data);  
			
			loadDecalSheetData();		  
			//trace(propSheet.getPropertySheet("global.properties").selectorNames);  
			//trace(propSheet.getPropertySheet("global.properties"));
			//trace(propSheet.getSelector(".TestStyle3",".TestStyle2",".TestStyle")); 
		}
		private function loadDecalSheetData():void{
			var urlLoader:URLLoader = new URLLoader();  
			urlLoader.addEventListener(Event.COMPLETE, onDecalSheetDataLoad, false, 0, true);  
			urlLoader.load(new URLRequest("xml/decalsheet.xml"));  
		}
		private function onDecalSheetDataLoad(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE, onDecalSheetDataLoad);  
			var xml:XML = XML(e.target.data);  
			var decalSheetManager:DecalSheetManager = GlobalDecalSheetManager.instance;  
			decalSheetManager.addEventListener(LoaderManagerEvent.PRELOAD_NEXT, onPreloadNext, false, 0, true);  
			decalSheetManager.addEventListener(LoaderManagerEvent.PRELOAD_DONE, onDecalSheetsLoad, false, 0, true);  
			decalSheetManager.parseXML(xml);  
		}
		protected function onPreloadNext(e:LoaderManagerEvent):void{
			trace("Loading DecalSheet " + e.data.totalPreloaded + " of " + e.data.totalPreloading);  
		}
		private function onDecalSheetsLoad(e:LoaderManagerEvent):void{
			var target:DecalSheetManager = e.target as DecalSheetManager;  
			target.removeEventListener(LoaderManagerEvent.PRELOAD_DONE, onDecalSheetsLoad);
			loadBrendAuto();
		}
		private function loadBrendAuto():void{
			var urlLoader:URLLoader = new URLLoader();  
			urlLoader.addEventListener(Event.COMPLETE, brendAutoLoad);  
			urlLoader.load(new URLRequest("xml/brend.xml"));  
		}
		private function brendAutoLoad(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE, brendAutoLoad);
			brendAutoXml = XML(e.target.data).children();
			loadStringUrl();
		}
		private function loadStringUrl():void{
			var urlLoader:URLLoader = new URLLoader();  
			urlLoader.addEventListener(Event.COMPLETE, stringUrlLoad);  
			urlLoader.load(new URLRequest("xml/settings.xml"));  
		}
		private function stringUrlLoad(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE, stringUrlLoad);
			init(brendAutoXml, XML(e.target.data));
		}
		protected function init(brendXml:XMLList, urlXml:XML):void{  
			
			var boxSelectAutoShadowing:DecalDisplay = new DecalDisplay("Box-SelectAuto_Shadowing");
			addChild(boxSelectAutoShadowing);
			
			var boxInternalPreloader:DecalDisplay = new DecalDisplay("Box-InternalPreloader");
			addChild(boxInternalPreloader);
				
			//var windowSelectAuto: WindowSelectAuto = new WindowSelectAuto(brendXml, urlXml);
			//addChild(windowSelectAuto);
			
			var windowWheelParameters: WindowWheelParameters = new WindowWheelParameters();
			addChild(windowWheelParameters);
			
			/*var ldr:Loader = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLdrLoaded);  
			ldr.load(new URLRequest("img/loader.gif"));
			
			function onLdrLoaded (e:Event):void {
				addChild(ldr);
			}*/
			/*boxSelectAutoShadowing.addEventListener(MouseEvent.CLICK, disable);
			
			function disable(e:MouseEvent):void{
				boxSelectAutoShadowing.visible = false;
			} */
		}
	}
}
